Popular Searches
Popular Course Categories
Popular Courses

Select Dropdown

Dropdowns & Advanced Elements

<div class="selenium-notes">

<h1>Select Dropdown in Selenium WebDriver</h1>

<p>A <strong>Select Dropdown</strong> is a common form control used on websites to allow users to choose one or more options from a predefined list. Examples include selecting a country, state, city, department, gender, category, month, year, payment method, or job role.</p>

<p>In Selenium WebDriver with Java, standard HTML dropdowns created using the <strong>&lt;select&gt;</strong> element can be handled conveniently using Selenium's <strong>Select</strong> class. The Select class provides methods such as <strong>selectByVisibleText()</strong>, <strong>selectByValue()</strong>, <strong>selectByIndex()</strong>, <strong>getOptions()</strong>, and methods for handling multiple selections.</p>

<p><strong>Selenium Training:</strong> <a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">Selenium Training Course</a></p>

<p><strong>Register for Course Demo:</strong> <a href="https://www.justacademy.co/register-for-course-demo" target="_blank" rel="noopener noreferrer">Register for Selenium Course Demo</a></p>

<hr>

<h2>1. What is a Select Dropdown?</h2>

<p>A select dropdown is an HTML form element that presents a list of predefined options. The standard HTML structure uses the <strong>&lt;select&gt;</strong> element as the container and one or more <strong>&lt;option&gt;</strong> elements as its choices.</p>

<pre><code>&lt;select id="country"&gt;

    &lt;option value="india"&gt;India&lt;/option&gt;

    &lt;option value="usa"&gt;United States&lt;/option&gt;

    &lt;option value="uk"&gt;United Kingdom&lt;/option&gt;

    &lt;option value="canada"&gt;Canada&lt;/option&gt;

&lt;/select&gt;</code></pre>

<p>In this example, <strong>country</strong> is the select element and each country is an option.</p>

<hr>

<h2>2. Why Dropdown Handling is Important in Selenium</h2>

<p>Dropdowns are frequently used in real-world web applications. Automated tests often need to select a particular value and then verify that the correct value has been selected.</p>

<ul>

<li>Country selection</li>

<li>State selection</li>

<li>City selection</li>

<li>Product category selection</li>

<li>Department selection</li>

<li>Month and year selection</li>

<li>Payment method selection</li>

<li>Language selection</li>

<li>Employment type selection</li>

<li>Registration form selections</li>

</ul>

<hr>

<h2>3. HTML Structure of a Select Dropdown</h2>

<pre><code>&lt;label for="department"&gt;Department&lt;/label&gt;

 

&lt;select id="department" name="department"&gt;

    &lt;option value="it"&gt;IT&lt;/option&gt;

    &lt;option value="hr"&gt;Human Resources&lt;/option&gt;

    &lt;option value="finance"&gt;Finance&lt;/option&gt;

    &lt;option value="sales"&gt;Sales&lt;/option&gt;

&lt;/select&gt;</code></pre>

<p>The important elements are:</p>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Element/Attribute</th><th>Purpose</th></tr>

<tr><td>&lt;select&gt;</td><td>Creates the dropdown</td></tr>

<tr><td>&lt;option&gt;</td><td>Represents an option inside the dropdown</td></tr>

<tr><td>id</td><td>Provides a unique identifier</td></tr>

<tr><td>name</td><td>Identifies the form field</td></tr>

<tr><td>value</td><td>Provides the option's submitted value</td></tr>

<tr><td>multiple</td><td>Allows multiple options to be selected</td></tr>

</table>

<hr>

<h2>4. Selenium Select Class</h2>

<p>Selenium provides the <strong>org.openqa.selenium.support.ui.Select</strong> class for interacting with standard HTML <strong>&lt;select&gt;</strong> elements. The class wraps a WebElement representing a select element and provides specialized dropdown operations.</p>

<p>Import the Select class using:</p>

<pre><code>import org.openqa.selenium.support.ui.Select;</code></pre>

<hr>

<h2>5. Basic Select Class Syntax</h2>

<pre><code>WebElement dropdown = driver.findElement(By.id("country"));

Select select = new Select(dropdown);</code></pre>

<p>After creating the Select object, selection methods can be called on it.</p>

<hr>

<h2>6. Complete Basic Example</h2>

<pre><code>WebElement countryDropdown =

        driver.findElement(By.id("country"));

 

Select country =

        new Select(countryDropdown);

 

country.selectByVisibleText("India");</code></pre>

<hr>

<h2>7. Select by Visible Text</h2>

<p><strong>selectByVisibleText()</strong> selects an option according to the text displayed to the user.</p>

<pre><code>WebElement dropdown =

        driver.findElement(By.id("country"));

 

Select select =

        new Select(dropdown);

 

select.selectByVisibleText("India");</code></pre>

<p>For example:</p>

<pre><code>&lt;select id="country"&gt;

    &lt;option value="in"&gt;India&lt;/option&gt;

    &lt;option value="us"&gt;United States&lt;/option&gt;

&lt;/select&gt;</code></pre>

<p>The following selects India:</p>

<pre><code>select.selectByVisibleText("India");</code></pre>

<hr>

<h2>8. Advantages of selectByVisibleText()</h2>

<ul>

<li>Easy to read.</li>

<li>Easy to understand.</li>

<li>Matches what the user sees.</li>

<li>Useful when option text is stable.</li>

<li>Makes test cases more readable.</li>

</ul>

<p>For most straightforward dropdown scenarios, visible text is often a clear way to express the intended selection.</p>

<hr>

<h2>9. Select by Value</h2>

<p><strong>selectByValue()</strong> selects an option by matching its HTML <strong>value</strong> attribute.</p>

<pre><code>&lt;select id="country"&gt;

    &lt;option value="in"&gt;India&lt;/option&gt;

    &lt;option value="us"&gt;United States&lt;/option&gt;

    &lt;option value="uk"&gt;United Kingdom&lt;/option&gt;

&lt;/select&gt;</code></pre>

<p>Java Selenium:</p>

<pre><code>Select select =

        new Select(driver.findElement(By.id("country")));

 

select.selectByValue("in");</code></pre>

<hr>

<h2>10. When to Use selectByValue()</h2>

<p>Use <strong>selectByValue()</strong> when the option's value attribute is stable and known.</p>

<pre><code>select.selectByValue("india");</code></pre>

<p>This method is useful when visible text changes but the underlying value remains stable.</p>

<hr>

<h2>11. Select by Index</h2>

<p><strong>selectByIndex()</strong> selects an option using its index.</p>

<pre><code>Select select =

        new Select(driver.findElement(By.id("country")));

 

select.selectByIndex(2);</code></pre>

<p>When using index-based selection, be careful if the application's option ordering can change.</p>

<hr>

<h2>12. Example of selectByIndex()</h2>

<pre><code>&lt;select id="city"&gt;

    &lt;option value="delhi"&gt;Delhi&lt;/option&gt;

    &lt;option value="mumbai"&gt;Mumbai&lt;/option&gt;

    &lt;option value="pune"&gt;Pune&lt;/option&gt;

    &lt;option value="bangalore"&gt;Bangalore&lt;/option&gt;

&lt;/select&gt;</code></pre>

<pre><code>Select city =

        new Select(driver.findElement(By.id("city")));

 

city.selectByIndex(2);</code></pre>

<p>Index-based selection should be used carefully because changes to option ordering can make the test select a different option.</p>

<hr>

<h2>13. Three Main Selection Methods</h2>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Method</th><th>Selection Basis</th><th>Example</th></tr>

<tr><td>selectByVisibleText()</td><td>Displayed option text</td><td>India</td></tr>

<tr><td>selectByValue()</td><td>HTML value attribute</td><td>in</td></tr>

<tr><td>selectByIndex()</td><td>Option index</td><td>2</td></tr>

</table>

<hr>

<h2>14. Difference Between the Three Selection Methods</h2>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Feature</th><th>Visible Text</th><th>Value</th><th>Index</th></tr>

<tr><td>Uses displayed text</td><td>Yes</td><td>No</td><td>No</td></tr>

<tr><td>Uses value attribute</td><td>No</td><td>Yes</td><td>No</td></tr>

<tr><td>Uses index</td><td>No</td><td>No</td><td>Yes</td></tr>

<tr><td>Easy to read</td><td>Very easy</td><td>Easy</td><td>Moderate</td></tr>

<tr><td>Depends on option order</td><td>No</td><td>No</td><td>Yes</td></tr>

<tr><td>Useful for dynamic option text</td><td>Sometimes</td><td>Often</td><td>Sometimes</td></tr>

</table>

<hr>

<h2>15. How Select Dropdown Handling Works</h2>

<pre><code>Locate &lt;select&gt; element

        |

        v

Create Select object

        |

        v

Choose selection strategy

        |

        +----------------------+

        |          |           |

        v          v           v

Visible Text    Value       Index

        |          |           |

        +----------+-----------+

                   |

                   v

            Option Selected

                   |

                   v

            Verify Selection</code></pre>

<hr>

<h2>16. Complete Dropdown Example</h2>

<pre><code>import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.Select;

 

public class DropdownExample {

 

    public static void main(String[] args) {

 

        WebDriver driver = new ChromeDriver();

 

        try {

            driver.manage().window().maximize();

            driver.get("https://example.com");

 

            WebElement countryDropdown =

                    driver.findElement(By.id("country"));

 

            Select country =

                    new Select(countryDropdown);

 

            country.selectByVisibleText("India");

 

        } finally {

            driver.quit();

        }

    }

}</code></pre>

<hr>

<h2>17. Getting All Dropdown Options</h2>

<p>The <strong>getOptions()</strong> method returns all options belonging to the select element.</p>

<pre><code>Select select =

        new Select(driver.findElement(By.id("country")));

 

List&lt;WebElement&gt; options =

        select.getOptions();</code></pre>

<p>Required import:</p>

<pre><code>import java.util.List;</code></pre>

<hr>

<h2>18. Printing All Dropdown Options</h2>

<pre><code>List&lt;WebElement&gt; options = select.getOptions();

 

for (WebElement option : options) {

    System.out.println(option.getText());

}</code></pre>

<p>This is useful for debugging and validating that expected options are present.</p>

<hr>

<h2>19. Complete Example to Print Options</h2>

<pre><code>WebElement dropdown =

        driver.findElement(By.id("country"));

 

Select select =

        new Select(dropdown);

 

List&lt;WebElement&gt; options =

        select.getOptions();

 

for (WebElement option : options) {

    System.out.println(

            "Option: " + option.getText()

    );

}</code></pre>

<hr>

<h2>20. Getting the First Selected Option</h2>

<p>The <strong>getFirstSelectedOption()</strong> method returns the first selected option. For a normal single-select dropdown, this represents the currently selected option.</p>

<pre><code>WebElement selected =

        select.getFirstSelectedOption();

 

System.out.println(

        selected.getText()

);</code></pre>

<hr>

<h2>21. Verifying the Selected Option</h2>

<pre><code>select.selectByVisibleText("India");

 

String selectedText =

        select.getFirstSelectedOption().getText();

 

System.out.println(

        "Selected: " + selectedText

);</code></pre>

<p>TestNG assertion:</p>

<pre><code>Assert.assertEquals(

        selectedText,

        "India"

);</code></pre>

<hr>

<h2>22. Getting All Selected Options</h2>

<p>The <strong>getAllSelectedOptions()</strong> method returns all currently selected options. For a normal single-select dropdown, this list generally contains one selected option; for a multiple-select dropdown, it can contain multiple options.</p>

<pre><code>List&lt;WebElement&gt; selectedOptions =

        select.getAllSelectedOptions();

 

for (WebElement option : selectedOptions) {

    System.out.println(option.getText());

}</code></pre>

<hr>

<h2>23. Checking Whether Dropdown Supports Multiple Selection</h2>

<p>The <strong>isMultiple()</strong> method checks whether the select element supports selecting multiple options.</p>

<pre><code>if (select.isMultiple()) {

    System.out.println(

            "Multiple selection is supported"

    );

} else {

    System.out.println(

            "Single selection only"

    );

}</code></pre>

<hr>

<h2>24. Single Select Dropdown</h2>

<p>A standard single-select dropdown allows one option to be selected at a time.</p>

<pre><code>&lt;select id="department"&gt;

    &lt;option value="it"&gt;IT&lt;/option&gt;

    &lt;option value="hr"&gt;HR&lt;/option&gt;

    &lt;option value="finance"&gt;Finance&lt;/option&gt;

&lt;/select&gt;</code></pre>

<p>Example:</p>

<pre><code>Select department =

        new Select(

                driver.findElement(

                        By.id("department")

                )

        );

 

department.selectByVisibleText("IT");</code></pre>

<hr>

<h2>25. Multiple Select Dropdown</h2>

<p>A multiple-select dropdown uses the <strong>multiple</strong> attribute and allows more than one option to be selected.</p>

<pre><code>&lt;select id="skills" multiple&gt;

    &lt;option value="java"&gt;Java&lt;/option&gt;

    &lt;option value="selenium"&gt;Selenium&lt;/option&gt;

    &lt;option value="sql"&gt;SQL&lt;/option&gt;

    &lt;option value="python"&gt;Python&lt;/option&gt;

&lt;/select&gt;</code></pre>

<hr>

<h2>26. Selecting Multiple Options</h2>

<p>The Select methods can be called repeatedly for a multiple-select dropdown.</p>

<pre><code>Select skills =

        new Select(

                driver.findElement(By.id("skills"))

        );

 

skills.selectByVisibleText("Java");

skills.selectByVisibleText("Selenium");

skills.selectByVisibleText("SQL");</code></pre>

<hr>

<h2>27. Deselect by Visible Text</h2>

<p>For multiple-select dropdowns, <strong>deselectByVisibleText()</strong> removes a selected option based on its displayed text.</p>

<pre><code>skills.deselectByVisibleText("SQL");</code></pre>

<hr>

<h2>28. Deselect by Value</h2>

<pre><code>skills.deselectByValue("selenium");</code></pre>

<p>This removes the selected option whose value attribute matches the supplied value.</p>

<hr>

<h2>29. Deselect by Index</h2>

<pre><code>skills.deselectByIndex(2);</code></pre>

<p>This is applicable to multiple-select dropdowns.</p>

<hr>

<h2>30. Deselect All</h2>

<p>The <strong>deselectAll()</strong> method clears all selected options. It is intended for multiple-select elements.</p>

<pre><code>skills.deselectAll();</code></pre>

<hr>

<h2>31. Selecting and Deselecting Multiple Options</h2>

<pre><code>Select skills =

        new Select(

                driver.findElement(By.id("skills"))

        );

 

skills.selectByVisibleText("Java");

skills.selectByVisibleText("Selenium");

skills.selectByVisibleText("SQL");

 

skills.deselectByVisibleText("SQL");</code></pre>

<hr>

<h2>32. Complete Multiple Select Example</h2>

<pre><code>WebElement skillsDropdown =

        driver.findElement(By.id("skills"));

 

Select skills =

        new Select(skillsDropdown);

 

skills.selectByVisibleText("Java");

skills.selectByVisibleText("Selenium");

skills.selectByVisibleText("SQL");

 

List&lt;WebElement&gt; selected =

        skills.getAllSelectedOptions();

 

for (WebElement option : selected) {

    System.out.println(

            "Selected: " + option.getText()

    );

}</code></pre>

<hr>

<h2>33. Select Class Works Only with &lt;select&gt;</h2>

<p>An important point is that Selenium's Java <strong>Select</strong> class is designed for actual HTML <strong>&lt;select&gt;</strong> elements. It should not be used with custom dropdowns implemented using elements such as <strong>&lt;div&gt;</strong>, <strong>&lt;li&gt;</strong>, buttons, or other custom components.</p>

<hr>

<h2>34. Standard Dropdown vs Custom Dropdown</h2>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Standard Dropdown</th><th>Custom Dropdown</th></tr>

<tr><td>Uses &lt;select&gt;</td><td>Often uses &lt;div&gt;, &lt;li&gt;, buttons, etc.</td></tr>

<tr><td>Contains &lt;option&gt;</td><td>May contain custom elements</td></tr>

<tr><td>Can use Select class</td><td>Select class does not apply</td></tr>

<tr><td>selectByVisibleText() available</td><td>Must interact with elements directly</td></tr>

<tr><td>selectByValue() available</td><td>Usually handled with locators/clicks</td></tr>

</table>

<hr>

<h2>35. Example of a Custom Dropdown</h2>

<pre><code>&lt;div class="dropdown"&gt;

    &lt;button&gt;Select Country&lt;/button&gt;

 

    &lt;div class="options"&gt;

        &lt;div&gt;India&lt;/div&gt;

        &lt;div&gt;United States&lt;/div&gt;

        &lt;div&gt;United Kingdom&lt;/div&gt;

    &lt;/div&gt;

&lt;/div&gt;</code></pre>

<p>This is not a standard <strong>&lt;select&gt;</strong> element, so the Select class should not be used.</p>

<hr>

<h2>36. Handling a Custom Dropdown</h2>

<p>For a custom dropdown, interact with the actual elements displayed by the application.</p>

<pre><code>driver.findElement(

        By.cssSelector(".dropdown button")

).click();

 

driver.findElement(

        By.xpath("//div[text()='India']")

).click();</code></pre>

<p>The exact locator depends on the application's HTML structure.</p>

<hr>

<h2>37. Why Select Class May Throw an Exception</h2>

<p>The Select constructor expects the supplied WebElement to represent a SELECT element. If the element is not a select element, Selenium can throw an <strong>UnexpectedTagNameException</strong>.</p>

<pre><code>WebElement element =

        driver.findElement(By.id("country"));

 

Select select = new Select(element);</code></pre>

<p>If the element is actually a div-based custom dropdown, the Select object should not be created for it.</p>

<hr>

<h2>38. Handling Dropdown Using ID Locator</h2>

<pre><code>Select country =

        new Select(

                driver.findElement(

                        By.id("country")

                )

        );

 

country.selectByVisibleText("India");</code></pre>

<hr>

<h2>39. Handling Dropdown Using Name Locator</h2>

<pre><code>Select country =

        new Select(

                driver.findElement(

                        By.name("country")

                )

        );

 

country.selectByVisibleText("India");</code></pre>

<hr>

<h2>40. Handling Dropdown Using CSS Selector</h2>

<pre><code>Select country =

        new Select(

                driver.findElement(

                        By.cssSelector("#country")

                )

        );

 

country.selectByVisibleText("India");</code></pre>

<hr>

<h2>41. Handling Dropdown Using XPath</h2>

<pre><code>Select country =

        new Select(

                driver.findElement(

                        By.xpath("//select[@id='country']")

                )

        );

 

country.selectByVisibleText("India");</code></pre>

<hr>

<h2>42. Handling Dropdown with WebElement Variable</h2>

<pre><code>WebElement countryElement =

        driver.findElement(

                By.id("country")

        );

 

Select country =

        new Select(countryElement);

 

country.selectByVisibleText("India");</code></pre>

<p>This approach is often easier to debug because the WebElement and Select objects are clearly separated.</p>

<hr>

<h2>43. Verifying All Expected Options</h2>

<p>Test automation may need to verify that required options are available.</p>

<pre><code>List&lt;WebElement&gt; options =

        country.getOptions();

 

for (WebElement option : options) {

    String text = option.getText();

    System.out.println(text);

}</code></pre>

<hr>

<h2>44. Checking Whether an Option Exists</h2>

<pre><code>boolean optionFound = false;

 

for (WebElement option : country.getOptions()) {

    if (option.getText().equals("India")) {

        optionFound = true;

        break;

    }

}

 

System.out.println(

        "India available: " + optionFound

);</code></pre>

<hr>

<h2>45. Selecting an Option Dynamically</h2>

<p>Sometimes the option to select is stored in a variable.</p>

<pre><code>String countryName = "India";

 

Select country =

        new Select(

                driver.findElement(

                        By.id("country")

                )

        );

 

country.selectByVisibleText(countryName);</code></pre>

<p>This makes the same automation method reusable for different test data.</p>

<hr>

<h2>46. Data-Driven Dropdown Selection</h2>

<pre><code>String[] countries = {

        "India",

        "United States",

        "United Kingdom"

};

 

for (String countryName : countries) {

 

    Select country =

            new Select(

                    driver.findElement(

                            By.id("country")

                    )

            );

 

    country.selectByVisibleText(countryName);

 

    System.out.println(

            "Selected: " + countryName

    );

}</code></pre>

<hr>

<h2>47. Dropdown with TestNG Assertion</h2>

<pre><code>WebElement countryElement =

        driver.findElement(By.id("country"));

 

Select country =

        new Select(countryElement);

 

country.selectByVisibleText("India");

 

String selected =

        country.getFirstSelectedOption()

               .getText();

 

Assert.assertEquals(

        selected,

        "India"

);</code></pre>

<hr>

<h2>48. Required Imports for Dropdown Testing</h2>

<pre><code>import java.util.List;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.Select;

 

import org.testng.Assert;</code></pre>

<hr>

<h2>49. Complete TestNG Dropdown Test</h2>

<pre><code>import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.Select;

import org.testng.Assert;

import org.testng.annotations.Test;

 

public class DropdownTest {

 

    @Test

    public void selectCountry() {

 

        WebDriver driver = new ChromeDriver();

 

        try {

            driver.manage().window().maximize();

            driver.get("https://example.com");

 

            WebElement countryElement =

                    driver.findElement(

                            By.id("country")

                    );

 

            Select country =

                    new Select(countryElement);

 

            country.selectByVisibleText("India");

 

            String selected =

                    country

                    .getFirstSelectedOption()

                    .getText();

 

            Assert.assertEquals(

                    selected,

                    "India"

            );

 

        } finally {

            driver.quit();

        }

    }

}</code></pre>

<hr>

<h2>50. Dropdown Selection Flow</h2>

<pre><code>Launch Browser

      |

      v

Open Web Page

      |

      v

Locate &lt;select&gt; Element

      |

      v

Create Select Object

      |

      v

Choose Selection Method

      |

      +-------------------------+

      |            |            |

      v            v            v

Visible Text    Value        Index

      |            |            |

      +------------+------------+

                   |

                   v

             Select Option

                   |

                   v

             Verify Selection

                   |

                   v

              Continue Test</code></pre>

<hr>

<h2>51. Dropdown Testing Scenarios</h2>

<p>Important test scenarios for dropdowns include:</p>

<ul>

<li>Verify dropdown is displayed.</li>

<li>Verify dropdown is enabled.</li>

<li>Verify expected options are present.</li>

<li>Select an option using visible text.</li>

<li>Select an option using value.</li>

<li>Select an option using index.</li>

<li>Verify the selected option.</li>

<li>Verify default selection.</li>

<li>Verify multiple-selection behavior where supported.</li>

<li>Verify disabled options.</li>

<li>Verify that invalid selections are not accepted.</li>

<li>Verify dependent dropdown behavior.</li>

</ul>

<hr>

<h2>52. Testing Default Selected Option</h2>

<p>A dropdown may have a default option selected when the page loads.</p>

<pre><code>WebElement dropdown =

        driver.findElement(By.id("country"));

 

Select select =

        new Select(dropdown);

 

String defaultOption =

        select.getFirstSelectedOption()

              .getText();

 

System.out.println(

        "Default: " + defaultOption

);</code></pre>

<hr>

<h2>53. Testing Disabled Dropdown</h2>

<p>A dropdown can be disabled using the HTML <strong>disabled</strong> attribute.</p>

<pre><code>&lt;select id="country" disabled&gt;

    &lt;option value="in"&gt;India&lt;/option&gt;

    &lt;option value="us"&gt;United States&lt;/option&gt;

&lt;/select&gt;</code></pre>

<p>Before interacting with a disabled dropdown, verify its state and ensure the test case reflects the expected application behavior.</p>

<hr>

<h2>54. Testing Disabled Options</h2>

<p>An individual option can also be disabled.</p>

<pre><code>&lt;select id="country"&gt;

    &lt;option value="in"&gt;India&lt;/option&gt;

    &lt;option value="us" disabled&gt;United States&lt;/option&gt;

&lt;/select&gt;</code></pre>

<p>A disabled option should not be treated as a valid selectable option in the test.</p>

<hr>

<h2>55. Dependent Dropdowns</h2>

<p>Many applications use dependent dropdowns. For example, selecting a country may dynamically populate a state dropdown.</p>

<pre><code>Country

   |

   v

Select India

   |

   v

State Dropdown Updated

   |

   v

Select Maharashtra

   |

   v

City Dropdown Updated

   |

   v

Select Mumbai</code></pre>

<p>In such cases, synchronization may be required between each selection and the appearance of the new options.</p>

<hr>

<h2>56. Example of Dependent Dropdown</h2>

<pre><code>Select country =

        new Select(

                driver.findElement(

                        By.id("country")

                )

        );

 

country.selectByVisibleText("India");

 

// Wait for state options to become available

 

Select state =

        new Select(

                driver.findElement(

                        By.id("state")

                )

        );

 

state.selectByVisibleText("Maharashtra");</code></pre>

<hr>

<h2>57. Waiting for Dynamic Dropdown Options</h2>

<p>If a dropdown is populated asynchronously after another selection, use an appropriate explicit wait rather than assuming the options are immediately available.</p>

<pre><code>WebDriverWait wait =

        new WebDriverWait(

                driver,

                Duration.ofSeconds(10)

        );

 

wait.until(

        d -&gt; d.findElements(

                By.cssSelector("#state option")

        ).size() &gt; 1

);</code></pre>

<p>Required imports:</p>

<pre><code>import java.time.Duration;

import org.openqa.selenium.support.ui.WebDriverWait;</code></pre>

<hr>

<h2>58. Dynamic Dropdown Example</h2>

<pre><code>country.selectByVisibleText("India");

 

WebDriverWait wait =

        new WebDriverWait(

                driver,

                Duration.ofSeconds(10)

        );

 

wait.until(

        d -&gt; d.findElements(

                By.cssSelector("#state option")

        ).size() &gt; 1

);

 

Select state =

        new Select(

                driver.findElement(

                        By.id("state")

                )

        );

 

state.selectByVisibleText("Maharashtra");</code></pre>

<hr>

<h2>59. Common Dropdown Exceptions</h2>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Exception</th><th>Possible Cause</th></tr>

<tr><td>UnexpectedTagNameException</td><td>Element is not a &lt;select&gt; element</td></tr>

<tr><td>NoSuchElementException</td><td>Requested option does not exist</td></tr>

<tr><td>StaleElementReferenceException</td><td>Dropdown or option was refreshed/replaced</td></tr>

<tr><td>ElementNotInteractableException</td><td>Element cannot currently be interacted with</td></tr>

<tr><td>TimeoutException</td><td>Dynamic dropdown did not become ready within the wait period</td></tr>

</table>

<hr>

<h2>60. UnexpectedTagNameException</h2>

<p>This exception commonly occurs when the Select class is used with a non-select element.</p>

<pre><code>WebElement element =

        driver.findElement(

                By.id("customDropdown")

        );

 

Select select =

        new Select(element);</code></pre>

<p>If <strong>customDropdown</strong> is a div, button, or another element instead of a select element, the Select class is inappropriate.</p>

<hr>

<h2>61. NoSuchElementException in Dropdown</h2>

<p>This can occur when the requested option cannot be found.</p>

<pre><code>select.selectByVisibleText("Australia");</code></pre>

<p>If the dropdown does not contain an option matching the requested text, the operation fails.</p>

<hr>

<h2>62. Handling Dropdowns with Page Object Model</h2>

<p>Dropdown interactions can be encapsulated inside page classes.</p>

<pre><code>public class RegistrationPage {

 

    private WebDriver driver;

 

    private By country =

            By.id("country");

 

    public RegistrationPage(

            WebDriver driver) {

 

        this.driver = driver;

    }

 

    public void selectCountry(

            String countryName) {

 

        Select select =

                new Select(

                        driver.findElement(country)

                );

 

        select.selectByVisibleText(

                countryName

        );

    }

 

    public String getSelectedCountry() {

 

        Select select =

                new Select(

                        driver.findElement(country)

                );

 

        return select

                .getFirstSelectedOption()

                .getText();

    }

}</code></pre>

<hr>

<h2>63. Using the Page Object</h2>

<pre><code>RegistrationPage page =

        new RegistrationPage(driver);

 

page.selectCountry("India");

 

Assert.assertEquals(

        page.getSelectedCountry(),

        "India"

);</code></pre>

<hr>

<h2>64. Reusable Dropdown Utility</h2>

<p>A framework can provide a reusable utility method for selecting an option by visible text.</p>

<pre><code>public static void selectByText(

        WebDriver driver,

        By locator,

        String text) {

 

    WebElement element =

            driver.findElement(locator);

 

    Select select =

            new Select(element);

 

    select.selectByVisibleText(text);

}</code></pre>

<p>Usage:</p>

<pre><code>DropdownUtils.selectByText(

        driver,

        By.id("country"),

        "India"

);</code></pre>

<hr>

<h2>65. Utility Method for Selecting by Value</h2>

<pre><code>public static void selectByValue(

        WebDriver driver,

        By locator,

        String value) {

 

    Select select =

            new Select(

                    driver.findElement(locator)

            );

 

    select.selectByValue(value);

}</code></pre>

<hr>

<h2>66. Utility Method for Selecting by Index</h2>

<pre><code>public static void selectByIndex(

        WebDriver driver,

        By locator,

        int index) {

 

    Select select =

            new Select(

                    driver.findElement(locator)

            );

 

    select.selectByIndex(index);

}</code></pre>

<hr>

<h2>67. Best Practice: Prefer Meaningful Selection</h2>

<p>Choose a selection strategy according to the application's HTML and test requirement.</p>

<ul>

<li>Use <strong>selectByVisibleText()</strong> when the displayed text is stable and meaningful.</li>

<li>Use <strong>selectByValue()</strong> when the value attribute is stable and appropriate.</li>

<li>Use <strong>selectByIndex()</strong> only when index-based selection is intentional and stable.</li>

</ul>

<hr>

<h2>68. Best Practice: Do Not Use Select for Custom Dropdowns</h2>

<p>Always inspect the HTML before using the Select class.</p>

<pre><code>&lt;select&gt;       --&gt; Use Select class

&lt;option&gt;       --&gt; Option inside Select

 

&lt;div&gt;          --&gt; Handle directly

&lt;button&gt;       --&gt; Handle directly

&lt;li&gt;           --&gt; Handle directly</code></pre>

<p>The Select class is intended for HTML select/option controls.</p>

<hr>

<h2>69. Best Practice: Avoid Hard-Coded Indexes When Possible</h2>

<p>This code may become fragile if the order of options changes:</p>

<pre><code>select.selectByIndex(3);</code></pre>

<p>If the test requirement is actually to select a named option, prefer:</p>

<pre><code>select.selectByVisibleText("India");</code></pre>

<hr>

<h2>70. Best Practice: Verify the Selection</h2>

<pre><code>select.selectByVisibleText("India");

 

String selected =

        select.getFirstSelectedOption()

              .getText();

 

Assert.assertEquals(

        selected,

        "India"

);</code></pre>

<p>Verification ensures that the intended selection was actually applied.</p>

<hr>

<h2>71. Best Practice: Use Explicit Waits for Dynamic Dropdowns</h2>

<p>For dropdowns whose options are populated after an AJAX or JavaScript operation, wait for the expected condition before selecting.</p>

<pre><code>WebDriverWait wait =

        new WebDriverWait(

                driver,

                Duration.ofSeconds(10)

        );

 

wait.until(

        d -&gt; d.findElements(

                By.cssSelector(

                        "#state option"

                )

        ).size() &gt; 1

);</code></pre>

<hr>

<h2>72. Best Practice: Keep Dropdown Logic Reusable</h2>

<p>In a larger Selenium framework, repeated dropdown operations should be placed into reusable utilities or Page Object methods.</p>

<pre><code>selectByText(driver, By.id("country"), "India");

selectByText(driver, By.id("state"), "Maharashtra");

selectByText(driver, By.id("city"), "Mumbai");</code></pre>

<p>This reduces duplicate code and makes test cases easier to maintain.</p>

<hr>

<h2>73. Common Mistakes in Select Dropdown Handling</h2>

<ul>

<li>Using Select on a custom dropdown.</li>

<li>Forgetting to import org.openqa.selenium.support.ui.Select.</li>

<li>Using incorrect option text.</li>

<li>Using the wrong value attribute.</li>

<li>Assuming an index will always remain unchanged.</li>

<li>Trying to deselect an option from a single-select dropdown.</li>

<li>Trying to select a disabled option.</li>

<li>Not waiting for dynamically loaded options.</li>

<li>Not verifying the selected option.</li>

<li>Using an incorrect locator for the select element.</li>

</ul>

<hr>

<h2>74. Incorrect vs Correct Dropdown Handling</h2>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Incorrect/Fragile</th><th>Better Approach</th></tr>

<tr><td>Use Select on a div dropdown</td><td>Interact with the custom dropdown elements</td></tr>

<tr><td>Always use index</td><td>Use visible text or value when appropriate</td></tr>

<tr><td>Select immediately after AJAX update</td><td>Wait for expected options</td></tr>

<tr><td>Do not verify selection</td><td>Use getFirstSelectedOption()</td></tr>

<tr><td>Use random locator</td><td>Use stable ID/name/CSS/XPath</td></tr>

<tr><td>Ignore disabled options</td><td>Verify enabled/disabled state when required</td></tr>

</table>

<hr>

<h2>75. Practical Project: Registration Form</h2>

<p><strong>Scenario:</strong> Automate a registration form containing country, state, city, department, and skills dropdowns.</p>

<h3>Test Steps</h3>

<ol>

<li>Launch the browser.</li>

<li>Open the registration page.</li>

<li>Locate the country dropdown.</li>

<li>Select India.</li>

<li>Wait for the state dropdown to update.</li>

<li>Select Maharashtra.</li>

<li>Wait for the city dropdown to update.</li>

<li>Select Mumbai.</li>

<li>Select a department.</li>

<li>Select multiple skills if the field supports multiple selections.</li>

<li>Verify the selected values.</li>

<li>Submit the form.</li>

<li>Verify the successful registration message.</li>

</ol>

<hr>

<h2>76. Registration Form Flow</h2>

<pre><code>Open Registration Page

        |

        v

Select Country

        |

        v

Wait for States

        |

        v

Select State

        |

        v

Wait for Cities

        |

        v

Select City

        |

        v

Select Department

        |

        v

Select Skills

        |

        v

Verify Selections

        |

        v

Submit Form

        |

        v

Verify Success</code></pre>

<hr>

<h2>77. Practical Project Code</h2>

<pre><code>WebDriver driver = new ChromeDriver();

 

try {

 

    driver.manage().window().maximize();

 

    driver.get("https://example.com/registration");

 

    Select country =

            new Select(

                    driver.findElement(

                            By.id("country")

                    )

            );

 

    country.selectByVisibleText("India");

 

    WebDriverWait wait =

            new WebDriverWait(

                    driver,

                    Duration.ofSeconds(10)

            );

 

    wait.until(

            d -&gt; d.findElements(

                    By.cssSelector(

                            "#state option"

                    )

            ).size() &gt; 1

    );

 

    Select state =

            new Select(

                    driver.findElement(

                            By.id("state")

                    )

            );

 

    state.selectByVisibleText("Maharashtra");

 

    wait.until(

            d -&gt; d.findElements(

                    By.cssSelector(

                            "#city option"

                    )

            ).size() &gt; 1

    );

 

    Select city =

            new Select(

                    driver.findElement(

                            By.id("city")

                    )

            );

 

    city.selectByVisibleText("Mumbai");

 

    Select department =

            new Select(

                    driver.findElement(

                            By.id("department")

                    )

            );

 

    department.selectByVisibleText("IT");

 

} finally {

 

    driver.quit();

}</code></pre>

<hr>

<h2>78. Dropdown Automation Checklist</h2>

<ul>

<li>☐ Inspect the dropdown HTML.</li>

<li>☐ Confirm whether it is a real &lt;select&gt; element.</li>

<li>☐ Identify the dropdown with a stable locator.</li>

<li>☐ Create a Select object.</li>

<li>☐ Choose visible text, value, or index.</li>

<li>☐ Verify the selected option.</li>

<li>☐ Handle multiple selection if applicable.</li>

<li>☐ Handle dynamic options with appropriate waits.</li>

<li>☐ Test disabled options when relevant.</li>

<li>☐ Use custom interaction for non-select dropdowns.</li>

<li>☐ Keep reusable dropdown logic in utilities/Page Objects.</li>

</ul>

<hr>

<h2>79. Select Dropdown Cheat Sheet</h2>

<pre><code>// Locate dropdown

WebElement element =

        driver.findElement(By.id("country"));

 

// Create Select object

Select select =

        new Select(element);

 

// Select by text

select.selectByVisibleText("India");

 

// Select by value

select.selectByValue("in");

 

// Select by index

select.selectByIndex(1);

 

// Get all options

List&lt;WebElement&gt; options =

        select.getOptions();

 

// Get selected option

WebElement selected =

        select.getFirstSelectedOption();

 

// Get all selected options

List&lt;WebElement&gt; selectedOptions =

        select.getAllSelectedOptions();

 

// Check multiple selection

boolean multiple =

        select.isMultiple();

 

// Deselect by text

select.deselectByVisibleText("India");

 

// Deselect by value

select.deselectByValue("in");

 

// Deselect by index

select.deselectByIndex(1);

 

// Deselect all

select.deselectAll();</code></pre>

<hr>

<h2>80. Interview Question: What is the Select Class in Selenium?</h2>

<p><strong>Answer:</strong> The Select class is a Selenium support class used to interact with standard HTML <strong>&lt;select&gt;</strong> dropdown elements. It provides methods for selecting, deselecting, retrieving options, and checking multiple-selection support.</p>

<hr>

<h2>81. Interview Question: How do you select an option by visible text?</h2>

<p><strong>Answer:</strong></p>

<pre><code>Select select =

        new Select(

                driver.findElement(

                        By.id("country")

                )

        );

 

select.selectByVisibleText("India");</code></pre>

<hr>

<h2>82. Interview Question: How do you select an option by value?</h2>

<p><strong>Answer:</strong></p>

<pre><code>select.selectByValue("in");</code></pre>

<hr>

<h2>83. Interview Question: How do you select an option by index?</h2>

<p><strong>Answer:</strong></p>

<pre><code>select.selectByIndex(2);</code></pre>

<hr>

<h2>84. Interview Question: Can Select handle a div-based dropdown?</h2>

<p><strong>Answer:</strong> No. Selenium's Select class is designed for HTML <strong>&lt;select&gt;</strong> elements. A custom dropdown implemented using elements such as div, li, or buttons must be handled by interacting with those elements directly.</p>

<hr>

<h2>85. Interview Question: How do you get all options from a dropdown?</h2>

<p><strong>Answer:</strong> Use <strong>getOptions()</strong>.</p>

<pre><code>List&lt;WebElement&gt; options =

        select.getOptions();</code></pre>

<hr>

<h2>86. Interview Question: How do you get the selected option?</h2>

<p><strong>Answer:</strong> Use <strong>getFirstSelectedOption()</strong>.</p>

<pre><code>WebElement selected =

        select.getFirstSelectedOption();</code></pre>

<hr>

<h2>87. Interview Question: How do you check whether a dropdown supports multiple selections?</h2>

<p><strong>Answer:</strong> Use <strong>isMultiple()</strong>.</p>

<pre><code>boolean multiple =

        select.isMultiple();</code></pre>

<hr>

<h2>88. Interview Question: Can we deselect an option from a single-select dropdown?</h2>

<p><strong>Answer:</strong> Deselect operations are intended for multiple-select dropdowns. A normal single-select dropdown does not support clearing an individual selection through the multiple-select deselection API.</p>

<hr>

<h2>89. Interview Question: What is the difference between selectByVisibleText and selectByValue?</h2>

<p><strong>Answer:</strong> <strong>selectByVisibleText()</strong> selects an option according to the text displayed to the user, whereas <strong>selectByValue()</strong> selects an option according to its HTML value attribute.</p>

<hr>

<h2>90. Interview Question: Which dropdown selection method should you use?</h2>

<p><strong>Answer:</strong> The appropriate method depends on the application. Use visible text when the displayed label is the stable identifier, value when the HTML value is stable and suitable, and index only when index-based selection is intentional and reliable.</p>

<hr>

<h2>91. Interview Question: What happens if the element passed to Select is not a select tag?</h2>

<p><strong>Answer:</strong> Selenium's Select constructor checks the element's tag name. If it is not a SELECT element, an <strong>UnexpectedTagNameException</strong> can be thrown.</p>

<hr>

<h2>92. Important Methods Summary</h2>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Method</th><th>Purpose</th></tr>

<tr><td>selectByVisibleText()</td><td>Select option by displayed text</td></tr>

<tr><td>selectByValue()</td><td>Select option by value attribute</td></tr>

<tr><td>selectByIndex()</td><td>Select option by index</td></tr>

<tr><td>getOptions()</td><td>Get all options</td></tr>

<tr><td>getFirstSelectedOption()</td><td>Get first/current selected option</td></tr>

<tr><td>getAllSelectedOptions()</td><td>Get all selected options</td></tr>

<tr><td>isMultiple()</td><td>Check multiple-selection support</td></tr>

<tr><td>deselectByVisibleText()</td><td>Deselect by visible text</td></tr>

<tr><td>deselectByValue()</td><td>Deselect by value</td></tr>

<tr><td>deselectByIndex()</td><td>Deselect by index</td></tr>

<tr><td>deselectAll()</td><td>Deselect all options in a multi-select</td></tr>

</table>

<hr>

<h2>93. Quick Revision</h2>

<pre><code>1. Inspect the dropdown.

2. Check whether it is a &lt;select&gt; element.

3. Locate the dropdown.

4. Create a Select object.

5. Select using:

   - selectByVisibleText()

   - selectByValue()

   - selectByIndex()

6. Verify the selected option.

7. For multiple select:

   - select multiple options

   - use deselect methods

8. For dynamic dropdowns:

   - use explicit waits

9. For custom dropdowns:

   - do not use Select class

10. Keep dropdown logic reusable.</code></pre>

<hr>

<h2>94. Final Conclusion</h2>

<p>Select Dropdown handling is an essential Selenium WebDriver skill because dropdown controls appear in registration forms, search forms, checkout pages, profile pages, administration panels, filters, and many other web applications.</p>

<p>For a standard HTML <strong>&lt;select&gt;</strong> element, Selenium's <strong>Select</strong> class provides a convenient API for selecting options by visible text, value, or index, retrieving available and selected options, and working with multiple-select controls.</p>

<p>The most important concept is to first inspect the HTML. If the control is a genuine <strong>&lt;select&gt;</strong> element, use the Select class. If the application uses a custom dropdown built with divs, buttons, list items, or JavaScript overlays, interact with the actual elements instead.</p>

<p>For reliable automation, use stable locators, select options using meaningful identifiers, wait for dynamically populated options, verify the selected value, and keep reusable dropdown logic inside utility or Page Object classes.</p>

<hr>

<h2>95. Course Resources</h2>

<p>To learn Selenium WebDriver, dropdown handling, locators, waits, browser automation, Page Object Model, TestNG, framework development, and practical automation testing, explore the following resources:</p>

<ul>

<li><a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">JustAcademy Selenium Training Course</a></li>

<li><a href="https://www.justacademy.co/register-for-course-demo" target="_blank" rel="noopener noreferrer">Register for Selenium Course Demo</a></li>

</ul>

</div>

Previous Next
whatsapp